cmake_minimum_required(VERSION 3.10)
project(my_ethercat_app)

# 设置 C++ 标准
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# 检查是否启用交叉编译
if(DEFINED ENV{CROSS_COMPILE})
    # 设置交叉编译工具链
    set(CMAKE_SYSTEM_NAME Linux)
    set(CMAKE_SYSTEM_PROCESSOR aarch64)
    
    # 设置交叉编译器路径
    set(TOOLCHAIN_PATH "/home/zqj/environment/gcc_rk3588/aarch64/gcc-arm-10.3-2021.07-x86_64-aarch64-none-linux-gnu")
    set(CMAKE_C_COMPILER "${TOOLCHAIN_PATH}/bin/aarch64-none-linux-gnu-gcc")
    set(CMAKE_CXX_COMPILER "${TOOLCHAIN_PATH}/bin/aarch64-none-linux-gnu-g++")
    
    # 设置查找路径
    set(CMAKE_FIND_ROOT_PATH "${TOOLCHAIN_PATH}/aarch64-none-linux-gnu")
    
    # 设置查找规则
    set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
    set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
    set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
    set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
    
    message(STATUS "启用交叉编译模式")
else()
    message(STATUS "使用本地编译模式")
endif()

# 设置编译选项
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra")

# 设置输出目录
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)

# 添加头文件路径
include_directories(
    ${CMAKE_CURRENT_SOURCE_DIR}
    /usr/local/out_igh/include
)

# 源文件
set(SOURCES
    my_ethercat_app2.cpp
    tinyxml2.cpp
)

# 创建可执行文件
add_executable(my_ethercat_app2 ${SOURCES})

# 直接指定库文件路径
target_link_libraries(my_ethercat_app2
    /usr/local/out_igh/lib/libethercat.a
    pthread
    rt
)

# 设置输出文件名
set_target_properties(my_ethercat_app2 PROPERTIES
    OUTPUT_NAME "my_ethercat_app2.bin"
)

# 添加自定义目标用于复制文件
add_custom_target(copy_to_share
    COMMAND ${CMAKE_COMMAND} -E copy_if_different
    ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/my_ethercat_app2.bin
    /mnt/hgfs/0VMware_share/
    COMMENT "复制可执行文件到共享目录"
    DEPENDS my_ethercat_app2
) 